package com.cheng.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: ChengJW
 * @date: 2021/11/6 13:15
 * @version: 1.0
 * @description: 日期工具类
 */
public class DateUtil {

    private static final Logger log =LoggerFactory.getLogger(DateUtil.class);

    /**
    * @author: ChengJW
    * @params: [date, format]
    * @return: java.lang.String
    * @date: 2021/11/6
    * @description: 日期格式转换成字符串格式
    */
    public static String dateToStr(Date date,String format){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static Date strToDate(String value,String format){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(value);
        } catch (ParseException e) {
            log.error("日期转换失败:"+e.getMessage(),e);
            e.printStackTrace();
        }
        return null;
    }
}
